package com.ruoyi.project.user.couple.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.couple.domain.Couple;
import com.ruoyi.project.user.couple.service.impl.CoupleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("user/couple")
public class CoupleController extends BaseController {
    private String prefix="user/couple";
    @Autowired
    private CoupleService coupleService;

    @RequiresPermissions("user:couple:view")
    @GetMapping()
    public String couple()
    {
        return prefix + "/couple";
    }

    /**
     * 查询新人列表
     * @param couple
     * @return
     */
    @RequiresPermissions ( "user:couple:list" )
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Couple couple)
    {
        startPage();
        List <Couple> list = coupleService.selectCoupleList ( couple );
        System.out.println ("list ========="+list);
        return getDataTable ( list );
    }

    /**
     * 导出新人列表
     * @param couple
     * @return
     */
    @RequiresPermissions("user:couple:export")
    @Log(title = "新人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Couple couple)
    {
        List<Couple> list = coupleService.selectCoupleList ( couple );
        ExcelUtil <Couple> util = new ExcelUtil<Couple>(Couple.class);
        return util.exportExcel(list, "couple");
    }

    /**
     * 添加新人
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存新人
     * @param couple
     * @return
     */
    @RequiresPermissions("user:couple:add")
    @Log(title = "新人", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Couple couple)
    {
        return toAjax(coupleService.insertCouple(couple));
    }

    /**
     * 修改新人
     * @param coupleId
     * @param mmap
     * @return
     */
    @GetMapping("/edit/{coupleId}")
    public String edit(@PathVariable("coupleId") Long coupleId, ModelMap mmap)
    {
        Couple couple = coupleService.selectCoupleById(coupleId);
        mmap.put("couple", couple);
        return prefix + "/edit";
    }

    /**
     * 修改保存
     * @param couple
     * @return
     */
    @RequiresPermissions("user:couple:edit")
    @Log(title = "新人", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Couple couple) {
       return  toAjax ( coupleService.updateCouple ( couple ) );
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("user:couple:remove")
    @Log(title = "新人", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(coupleService.deleteCoupleByIds(ids));
    }

    @RequiresPermissions ( "user:couple:state" )
    @PostMapping("/updateState")
    @ResponseBody
    public AjaxResult updateState(Couple couple){
        return toAjax ( coupleService.updateState ( couple ));
    }

}
